package cn.com.dhcc.turbo.service.exch.filedata;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.dao.exch.RecvFileDataDao;
import cn.com.dhcc.turbo.entity.exch.RecvFileData;

@Service
public class RecvFileDataService extends AbstractCrudService<RecvFileData> {
    @SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.R);

    @Autowired
    private RecvFileDataDao dao;

    @Override
    public CrudDao<RecvFileData> getCrudDao() {
        return dao;
    }

    /**
     * 根据pid获取获取文件列表
     *
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<RecvFileData> getListByGroupId(String pid) throws ServiceException {
        WhereCondition parameter = new WhereCondition();
        parameter.where().eq("group_id", pid);
        return this.list(parameter);
    }

    
    /**
     * 获取尚未发送的数据列表
     *
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<RecvFileData> getProcessLost() throws ServiceException {
    	Integer t=2;
        WhereCondition parameter = new WhereCondition();
        parameter.where().leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.SENT.value())
        .or().eq("SEND_STATUS", AppConst.SendStatus.FAIL.value())
        .rightBracket()
        .and().eq("NOTIFY_STATUS", AppConst.NO)
        .and().greaterThanOrEq("TO_CHAR(SYSDATE, 'MI')-TO_CHAR(modified, 'MI')",t.toString() );
        return this.list(parameter);
    }

    /**
     * 获取尚未发送的数据列表
     *
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<RecvFileData> getUnsentList() throws ServiceException {
        WhereCondition parameter = new WhereCondition();
        parameter.where().notEq("SEND_STATUS", AppConst.SendStatus.SENT.value())
        .and().notEq("SEND_STATUS", AppConst.SendStatus.FAIL.value())
        .and().notEq("SEND_STATUS", AppConst.SendStatus.REMOTE_SENDING.value());
        return this.list(parameter);
    }

    /**
     * 批量更新数据发送状态
     *
     * @param dataList
     */
    public int updateSendStatus(List<RecvFileData> dataList, String status) throws NiceException {
        try {
            return dao.updateSendStatus(dataList, status);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }
    /**
     * 批量更新数据发送状态
     *
     * @param dataList
     */
    public int updateNotifyStatus(List<RecvFileData> dataList, String status) throws NiceException {
        try {
            return dao.updateNotifyStatus(dataList, status);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }
    public String getRecvCount(String syscode, Date begintime, Date endtime) throws NiceException {
        try {
            return dao.getRecvCount(syscode, begintime, endtime);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }


    public String getRecvSuccess(String syscode) throws NiceException {
        try {
            return dao.getRecvSuccess(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    public String getRecvFail(String syscode) throws NiceException {
        try {
            return dao.getRecvFail(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }
    
    /**
  	 * 获取发送节点列表
  	 * 
  	 * @return
  	 */
  	public List<Map<String, Object>> getSendNodeList() throws NiceException {
  		try {
  			return dao.getSendNodeList();
  		} catch (DaoException e) {
  			logger.error(e);
  			throw new NiceException(e);
  		}
  	}

  	/**
  	 * 获取接收节点列表
  	 * 
  	 * @return
  	 */
  	public List<Map<String, Object>> getRecvNodeList() throws NiceException {
  		try {
  			return dao.getRecvNodeList();
  		} catch (DaoException e) {
  			logger.error(e);
  			throw new NiceException(e);
  		}
  	}

	/**
	 * 获取发送适配器列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws NiceException {
		try {
			return dao.getSendAdapterList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}
	

}
